package com.creat.wiki.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *拦截器：Spring框架特有的，常用于登录校验，权限校验，请求日志打印
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger LOG = LoggerFactory.getLogger(LoginInterceptor.class);

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //打印请求信息
        LOG.info("-------------- LoginInterceptor 开始 --------------");
        long startTime = System.currentTimeMillis();
        request.setAttribute("requestStartTime",startTime);
        //OPTIONS请求不做校验
        //前后端分离的架构，前端会发一个OPTIONS请求先做预检，对预检请求不处理
        if (request.getMethod().toUpperCase().equals("OPTIONS")){
            return true;
        }

        String path = request.getRequestURI().toString();
        LOG.info("端口登录拦截,path:{}", path);

        //获取header的token参数
        String token = request.getHeader("token");
        LOG.info("登路校验开始，token:{}", token);
        if (token == null || token.isEmpty()){
            LOG.info("token");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return false;
        }
        Object object = redisTemplate.opsForValue().get(token);
        if (object == null) {
            LOG.warn("token无效，请求被拦截");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return false;
        }else {
            LOG.info("已登录:{}", object);
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (Long) request.getAttribute("requestStartTime");
        LOG.info("-------------- LogInterceptor 结束 耗时：{}ms --------------", System.currentTimeMillis() - startTime);
    }
}
